<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Sales\Data;

use Admin\Data\Common;
use Sales\Entity\Order;

class OrderState
{
    private static $deliveryTrackState;

    /**
     * 订单付款，判断是否可以付款
     * @param Order $orderInfo
     * @return bool
     */
    public static function payOrder(Order $orderInfo)
    {
        if (
            empty($orderInfo)
            || $orderInfo->getOrderStatus() > Common::orderStatusCode('WAIT_GOODS_ARRIVE')
            || $orderInfo->getPaymentType() == 'CashOnDelivery'
        ) return false;

        return true;
    }

    /**
     * 订单发货，判断是否可以进行发货处理
     * @param Order $orderInfo
     * @return bool
     */
    public static function shipOrder(Order $orderInfo)
    {
        if (
            empty($orderInfo)
            || $orderInfo->getDeliveryType() == 'selfFetch'
            || $orderInfo->getOrderStatus() > Common::orderStatusCode('FINISH_DISTRIBUTION_GOODS')
            || ($orderInfo->getOrderStatus() <= Common::orderStatusCode('WAIT_GOODS_ARRIVE') && $orderInfo->getPaymentType() != 'CashOnDelivery')
        ) return false;

        return true;
    }

    /**
     * 判断订单内的虚拟商品是否可以发货
     * @param Order $orderInfo
     * @return bool
     */
    public static function sendOrderGoodsVirtual(Order $orderInfo)
    {
        if (
            empty($orderInfo)
            || $orderInfo->getOrderStatus() > Common::orderStatusCode('FINISH_DISTRIBUTION_GOODS')
            || $orderInfo->getOrderStatus() <= Common::orderStatusCode('WAIT_GOODS_ARRIVE')
        ) return false;

        return true;
    }

    /**
     * 判断是否可以修改订单快递单号
     * @param Order $orderInfo
     * @return bool
     */
    public static function editOrderExpressNumber(Order $orderInfo)
    {
        if (
            empty($orderInfo)
            || $orderInfo->getOrderStatus() < Common::orderStatusCode('GOODS_SHIPPED')
            || $orderInfo->getOrderStatus() >= Common::orderStatusCode('GOODS_RECEIVED')
        ) return false;

        return true;
    }

    /**
     * 订单收货，判断是否可以进行收货操作
     * @param Order $orderInfo
     * @return bool
     */
    public static function receiptOrder(Order $orderInfo)
    {
        if (
            empty($orderInfo)
            || (
                $orderInfo->getDeliveryType() != 'selfFetch'
                && ($orderInfo->getOrderStatus() < Common::orderStatusCode('GOODS_SHIPPED') || $orderInfo->getOrderStatus() >= Common::orderStatusCode('GOODS_RECEIVED'))
            )
            || (
                $orderInfo->getDeliveryType() == 'selfFetch'
                && ($orderInfo->getOrderStatus() < Common::orderStatusCode('WAIT_GOODS_ARRIVE') || $orderInfo->getOrderStatus() >= Common::orderStatusCode('GOODS_RECEIVED'))
            )
        ) return false;

        return true;
    }

    /**
     * 取消订单，判断是否可以进行处理
     * @param Order $orderInfo
     * @return bool
     */
    public static function cancelOrder(Order $orderInfo)
    {
        $paymentConfig = null;
        if (in_array($orderInfo->getPaymentCode(), Common::REFUND_PAYMENT_ARRAY)) $paymentConfig = Common::getPaymentConfig($orderInfo->getPaymentCode());

        if (
            empty($orderInfo)
            || (
                $orderInfo->getPaymentType() == 'OnlinePay'
                && (
                    ((!isset($paymentConfig['cancelOrderState']['checked']) || $paymentConfig['cancelOrderState']['checked'] != 1) && $orderInfo->getOrderStatus() >= Common::orderStatusCode('WAIT_GOODS_ARRIVE'))
                    || (isset($paymentConfig['cancelOrderState']['checked']) && $paymentConfig['cancelOrderState']['checked'] == 1 && $orderInfo->getOrderStatus() >= Common::orderStatusCode('FINISH_DISTRIBUTION_GOODS'))
                )
            )
            || ($orderInfo->getPaymentType() == 'CashOnDelivery' && $orderInfo->getOrderStatus() >= Common::orderStatusCode('GOODS_SHIPPED'))
            || $orderInfo->getOrderStatus() == Common::orderStatusCode('CANCEL_ORDER')
            || ($orderInfo->getPaymentType() == 'OfflinePay' && $orderInfo->getOrderStatus() >= Common::orderStatusCode('FINISH_PAYMENT'))
        ) return false;

        return true;
    }

    /**
     * 物流轨迹，判断是否可查看物流轨迹
     * @param Order $orderInfo
     * @param array $deliveryIdArray
     * @return bool
     */
    public static function orderExpressTrack(Order $orderInfo, array $deliveryIdArray = [])
    {
        if (self::$deliveryTrackState == null) self::$deliveryTrackState = Common::configValue('deliveryApiState', 'delivery');

        if (
            $orderInfo->getDeliveryType() == 'delivery'
            && self::$deliveryTrackState == 1
            && (
                $orderInfo->getDeliveryId() <= 0
                || (!empty($deliveryIdArray) && isset($deliveryIdArray[$orderInfo->getDeliveryId()]))
            )
        ) return true;

        return false;
    }
}